/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author marcomochito
 */
public class Ventana2 extends JFrame implements ActionListener, ChangeListener{

    private JPanel pnlPreguntas, pnlRespuestas, pnlBotones, pnlResp, pnlContenedor, pnlCont, pnlTitulo;
    private JLabel lblPregunta, lblTipoPregunta,lblNumPreg, lblLitResp, lblPistas, lblTitulo, lblIndicaciones;
    private JTextField txtPregunta, txtCorrectas, txtTitulo, txtIndicaciones;
    private JComboBox cboTipo;
    private JSpinner spnNumPreg, spnAgrOpc;
    private JCheckBox chk;
    private JButton btnAgrOpc, btnGuardarTodo, btnAgrPreg;
    private JScrollPane spRespuestas, spContenedor;
    private List<JPanel> lstPanel = new ArrayList<>();
    private List<JPanel> lstPreg = new ArrayList<>();
    private List<JScrollPane> lstResp = new ArrayList<>();
    
    int i=0, j=0;
    
    Ventana2() {
        super("Cuestionario");
        this.setSize(1200, 600);
        //this.setDefaultCloseOperation(0);
        this.setLayout(new BorderLayout(0,0));
        pnlTitulo = new JPanel();
        this.add(pnlTitulo, BorderLayout.NORTH);
        pnlTitulo.setLayout(new BoxLayout(pnlTitulo, BoxLayout.Y_AXIS));
        pnlTitulo.add(lblTitulo = new JLabel("Titulo"));
        pnlTitulo.add(txtTitulo = new JTextField(50));
        pnlTitulo.add(lblIndicaciones = new JLabel("Indicaciones"));
        pnlTitulo.add(txtIndicaciones = new JTextField(70));
        pnlContenedor = new JPanel();
        //pnlContenedor.setBorder(new TitledBorder("Contenedor"));
        spContenedor = new JScrollPane(pnlContenedor);
        pnlContenedor.setLayout(new BoxLayout(pnlContenedor, BoxLayout.Y_AXIS));        
        this.add(spContenedor, BorderLayout.CENTER);
 
        pnlBotones = new JPanel();
        pnlBotones.setBorder(new TitledBorder("Seleccione"));
        this.add(pnlBotones, BorderLayout.SOUTH);
        pnlBotones.setLayout(new BoxLayout(pnlBotones, BoxLayout.X_AXIS));
//        btnAgrOpc = new JButton("Agregar Opción");
        btnGuardarTodo = new JButton("Guardar Todo");
        //pnlBotones.add(btnAgrOpc);
        pnlBotones.add(btnAgrPreg = new JButton("Agregar Pregunta"));
        
        pnlBotones.add(btnGuardarTodo);
        //btnAgrPreg.addActionListener(this);
//        btnAgrOpc.addActionListener(this);
        btnAgrPreg.addActionListener(new ActionListener(){@Override public void actionPerformed(ActionEvent e){
            evento (e);
        }
        });
        btnGuardarTodo.addActionListener(this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btnAux = (JButton) e.getSource();
        System.out.println("Clic en el "+btnAux.getText());
        
        if(e.getSource() == btnAgrOpc){
            //JButton btnAux2 = (JButton) e.getSource();
            i++;
            pnlResp = new JPanel();
            pnlResp.setBounds(10, 20, 570, 60);
            pnlResp.setBorder(new TitledBorder("Opcion "+(i)));
            lstPanel.add(pnlResp);
            pnlResp.setLayout(new BoxLayout(pnlResp, BoxLayout.X_AXIS));
            chk = new JCheckBox();
            chk.setBounds(10, 20, 100, 30);
            pnlResp.add(chk);
            txtCorrectas = new JTextField(10);
            txtCorrectas.setBounds(110, 20, 200, 30);
            pnlResp.add(txtCorrectas);
            pnlResp.updateUI();
            Iterator <JPanel> itera = lstPanel.listIterator();
            while(itera.hasNext()){
                //pnlRespuestas.updateUI();
                pnlRespuestas.add(itera.next());
                pnlRespuestas.updateUI();
            }
            spRespuestas.revalidate();
            spRespuestas.updateUI();
        }
        
        if(e.getSource() == btnGuardarTodo){
            
        }
    }
    
    public void evento(ActionEvent e){

        //if(e.getSource() == btnAgrPreg){
            j++;
            pnlCont = new JPanel();
            pnlCont.setBorder(new TitledBorder("Pregunta "+j));
            lstPreg.add(pnlCont);
            pnlCont.setLayout(new GridBagLayout());
            GridBagConstraints grid = new GridBagConstraints();
            grid.gridx = 0; grid.gridy = 0; grid.gridheight = 1; grid.gridwidth = 1;
            grid.fill = GridBagConstraints.HORIZONTAL;
            lblPregunta = new JLabel("Ingrese Pregunta");
            pnlCont.add(lblPregunta, grid);
            grid.gridx = 1; grid.gridy = 0; grid.gridheight = 1; grid.gridwidth = 2;
            grid.fill = GridBagConstraints.HORIZONTAL;
            txtPregunta = new JTextField(30);
            pnlCont.add(txtPregunta, grid);
            grid.gridx = 3; grid.gridy = 0; grid.gridheight = 1; grid.gridwidth = 1;
            grid.fill = GridBagConstraints.HORIZONTAL;
            btnAgrOpc = new JButton("Agregar Opción");
            pnlCont.add(btnAgrOpc, grid);
            btnAgrOpc.addActionListener(this);
            grid.gridx = 0; grid.gridy = 1; grid.gridheight = 1; grid.gridwidth = 1;
            grid.fill = GridBagConstraints.HORIZONTAL;
            cboTipo = new JComboBox();
            cboTipo.addItem("Seleccion Multiple");
            cboTipo.addItem("Respuesta Corta");
            cboTipo.addItem("Test");
            cboTipo.addItem("Completar");
            pnlCont.add(cboTipo, grid);
            grid.gridx = 1; grid.gridy = 2; grid.gridheight = 3; grid.gridwidth = 4;
            //grid.fill = GridBagConstraints;
            pnlRespuestas = new JPanel();
            spRespuestas = new JScrollPane(pnlRespuestas);
            pnlCont.add(spRespuestas, grid);
            pnlRespuestas.setBorder(new TitledBorder("Respuesta"));
            pnlRespuestas.setLayout(new BoxLayout(pnlRespuestas, BoxLayout.Y_AXIS));
            Iterator<JPanel>itera = lstPreg.listIterator();
            while(itera.hasNext()){
                pnlContenedor.add(itera.next());
            }
            //spContenedor.validate();
            spContenedor.revalidate();
            //spContenedor.repaint();
            //spContenedor.updateUI();
        //}
        
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
